package com.istock.union.user.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.istock.base.cache.UseCache;
import com.istock.base.enumration.BaseEnum;
import com.istock.base.enumration.EnumsDataProvider;
import com.istock.union.user.dao.DictDataInfoMapper;
import com.istock.union.user.model.DictDataInfo;
import com.istock.union.user.model.DictDataInfoExample;

@Repository("EnumProvider")
public class EnumProvider implements EnumsDataProvider {
	@Autowired
	private DictDataInfoMapper mapper;
	
	public void setMapper(DictDataInfoMapper mapper) {
		this.mapper = mapper;
	}

	@Override
	@UseCache(key="'DIC_'+#type" , expireTime=60*60*24)
	public List<BaseEnum> getEnumsByType(String type) {
		DictDataInfoExample example = new DictDataInfoExample();
		DictDataInfoExample.Criteria criteria = example.createCriteria();
		criteria.andDictTypeEqualTo(type);
		example.setOrderByClause("DICT_SORT ASC");
		List<DictDataInfo> dataList = mapper.selectByExample(example);
		if(dataList != null && dataList.size()>0){
			List<BaseEnum> result = new ArrayList<BaseEnum>();
			for(DictDataInfo data : dataList){
				result.add(new BaseEnum(data.getDictValue() , data.getDictType() ,data.getDictCode() , data.getCssClass(), data.getDictLabel()));
			}
			return result;
		}
		return null;
	}

}
